package org.abratuhi.kvngroupware.gui;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.SpinnerListModel;
import javax.swing.SpringLayout;
import javax.swing.table.DefaultTableModel;

import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.controller.JokeManagerController;
import org.abratuhi.kvngroupware.model.Joke;
import org.abratuhi.kvngroupware.spring.SpringUtilities;

@SuppressWarnings("serial")
public class GUIJokeManager extends JPanel{
	
	public final static String AC_ADD_JOKE = "ac_jokemanager_addjoke";
	public final static String AC_UPDATE_JOKE = "ac_jokemanager_updatejoke";
	public final static String AC_RATE_JOKE = "ac_jokemanager_ratejoke";
	
	DefaultTableModel tableJokesModel = new DefaultTableModel();
	String[] tableColumns;
	public JTable tableJokes = new JTable(tableJokesModel);
	JTextArea textJoke = new JTextArea();
	public int selectedJoke = -1;
	
	JButton buttonAdd = new JButton();
	JButton buttonUpdate = new JButton();
	JButton buttonRate = new JButton();
	JSpinner spinnerRating = new JSpinner(new SpinnerListModel(Joke.RATINGS));
	
	JFrame frame;
	
	KVNGroupwareClient kvnclient;
	JokeManagerController controller;
	ArrayList<Joke> jokes;
	
	public GUIJokeManager(KVNGroupwareClient kvnclient, ArrayList<Joke> jokes, Properties locale){
		super();
		
		this.kvnclient = kvnclient;
		this.jokes = jokes;
		this.controller = new JokeManagerController(this.kvnclient, this, this.jokes);
		
		this.buttonAdd.setText(locale.getProperty("jokesmanager.button.add_joke"));
		this.buttonUpdate.setText(locale.getProperty("jokesmanager.button.update_joke"));
		this.buttonRate.setText(locale.getProperty("jokesmanager.button.rate_joke"));
		this.tableColumns = locale.getProperty("jokesmanager.columns").split(",");
		
		buttonAdd.setActionCommand(AC_ADD_JOKE);
		buttonUpdate.setActionCommand(AC_UPDATE_JOKE);
		buttonRate.setActionCommand(AC_RATE_JOKE);
		
		tableJokes.getSelectionModel().addListSelectionListener(controller);
		buttonAdd.addActionListener(controller);
		buttonUpdate.addActionListener(controller);
		buttonRate.addActionListener(controller);
		
		for(int i=0; i<tableColumns.length; i++) tableJokesModel.addColumn(tableColumns[i]);
		tableJokes.getColumnModel().getColumn(0).setPreferredWidth(50);
		tableJokes.getColumnModel().getColumn(0).setMaxWidth(50);
		tableJokes.getColumnModel().getColumn(1).setMinWidth(200);
		tableJokes.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		textJoke.setColumns(20);
		textJoke.setRows(6);
		
		Box boxB1 = Box.createHorizontalBox();
		boxB1.add(Box.createGlue());
		boxB1.add(buttonAdd);
		boxB1.add(buttonUpdate);
		boxB1.add(buttonRate);
		boxB1.add(spinnerRating);
		boxB1.add(Box.createGlue());
		
		this.setLayout(new SpringLayout());
		this.add(new JScrollPane(tableJokes));
		this.add(new JScrollPane(textJoke));
		this.add(boxB1);
		SpringUtilities.makeCompactGrid(this, 3, 1, 0, 0, 0, 0);
	}
	
	public void paint(Graphics g){
		super.paint(g);
		
		//selectedJoke = getSelectedIndex();
		
		int nrows = tableJokesModel.getRowCount();
		for(int i=0; i<nrows; i++){
			tableJokesModel.removeRow(0);
		}
		
		for(int i=0; i<jokes.size(); i++){
			tableJokesModel.addRow(new String[]{jokes.get(i).getRating(), jokes.get(i).getText()});
		}
		
		if(selectedJoke != -1) tableJokes.getSelectionModel().setLeadSelectionIndex(selectedJoke);
	}
	
	public void setJokeText(String text){
		textJoke.setText(text);
	}
	
	public String getJokeText(){
		return textJoke.getText();
	}
	
	public String getJokeRating(){
		return (String) spinnerRating.getValue();
	}
	
	public JFrame getGUI(){
		return frame;
	}

	public void createJFrame(){
		frame = new JFrame("..:: KVN Groupware Client ::..");
		frame.getContentPane().add(this);
		frame.addWindowListener(controller);
		frame.setVisible(true);
		frame.pack();
	}
	
	public void disposeJFrame(){
		frame.dispose();
	}
}
